/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FacturaDetalles.java
 *
 * Created on 26/02/2011, 12:01:38 AM
 */
package com.catosist.run.application.viaje.factura;

import com.catosist.run.application.viaje.factura.facturadetalle.DialogFacturaDetalle;
//import com.catosist.run.application.viaje.factura.guia.ManGuia;
import com.catosist.run.application.venta.guia.ManGuia;
import com.catosist.run.business.gestorComprobanteDetalle.GestorComprobanteDetalle;
import com.catosist.run.business.gestorcliente.GestorCliente;
import com.catosist.run.business.gestorcomprobante.GestorComprobante;
import com.catosist.run.services.ClienteDTO;
import com.catosist.run.services.ComprobanteDTO;
import com.catosist.run.services.ComprobanteDetalleDTO;
import com.catosist.run.services.ViajeDTO;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Locale;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.springframework.dao.DataIntegrityViolationException;

/**
 *
 * @author igorov
 */
public class FacturaDetalles extends javax.swing.JFrame {

    /** Creates new form FacturaDetalles */
    public FacturaDetalles() {
        initComponents();
    }

    public FacturaDetalles(GestorComprobante gestorComprobante, int facturaId, int viajeId, ManFactura manFactura) {
        this.manFactura = manFactura;
        initComponents();
        iniciar();
        this.gestorComprobante = gestorComprobante;
        this.comprobanteId = facturaId;
        this.viajeId = viajeId;

//        DefaultComboBoxModel modelCmbCliente = new DefaultComboBoxModel();
//        for (Iterator<ClienteDTO> it = gestorCliente.getAll().iterator(); it.hasNext();) {
//            ClienteDTO clienteDTO = it.next();
//            modelCmbCliente.addElement(clienteDTO.getNombre());
//        }
//        this.cmbCliente.setModel(modelCmbCliente);

        llenar_datos();
        

        model = new DefaultTableModel();
        
        model.addColumn("ID");
        model.addColumn("Cantidad");
        model.addColumn("Producto");
        model.addColumn("Precio Unitario");
        model.addColumn("Total");

        refresh();

    }

    public JPanel getPanel(){
        return jPanel1;
    }

    public void iniciar() {
//        lista_comprobantes = new LinkedList<ComprobanteDetalleDTO>();
//        lista_comprobantes_borrados = new LinkedList<ComprobanteDetalleDTO>();
        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorComprobanteDetalle = (GestorComprobanteDetalle) ctx.getBean("gestorComprobanteDetalle");
        gestorCliente = (GestorCliente) ctx.getBean("gestorCliente");
    }

    public void refresh() {
//        if (facturaId != 0) {
//            ComprobanteDTO comprobanteDTO = (ComprobanteDTO) gestorComprobante.getById(facturaId);
//
//            txtNumeroFactura.setText(String.valueOf(comprobanteDTO.getNumero()));
//            txtDireccion.setText(comprobanteDTO.getDireccion());
//            txtRUC.setText(comprobanteDTO.getRuc());
//            txtNombre.setText(comprobanteDTO.getNombre());
//            dtcFecha.setDate(comprobanteDTO.getFingreso());
//            lblTotal.setText(String.valueOf(comprobanteDTO.getTotal()));
//
//            List<ComprobanteDetalleDTO> comprobanteDetalleDTOs = gestorComprobanteDetalle.getByComprobanteId(facturaId);
//
//            for (Iterator<ComprobanteDetalleDTO> it = comprobanteDetalleDTOs.iterator(); it.hasNext();) {
//                ComprobanteDetalleDTO comprobanteDetalleDTO = it.next();
//                model.addRow(comprobanteDetalleDTO.toArray());
////                total += comprobanteDTO.getTotal();
//            }
////            this.lblTotal.setText("Total: " +String.valueOf(total));
//        }
        lista_comprobantes = gestorComprobanteDetalle.getByComprobanteId(comprobanteId);
        System.out.println("REFRESH DE FCTURADETALLES");
        model = new DefaultTableModel();

        model.addColumn("ID");
        model.addColumn("Cantidad");
        model.addColumn("Producto");
        model.addColumn("Precio Unitario");
        model.addColumn("Total");

        for (Iterator<ComprobanteDetalleDTO> it = lista_comprobantes.iterator(); it.hasNext();) {

                ComprobanteDetalleDTO comprobanteDetalleDTO = it.next();
                model.addRow(comprobanteDetalleDTO.toArray());
//                total += comprobanteDTO.getTotal();
        }
//        lblTotal.setText(String.valueOf(totalComprobante));
        this.jTable1.setModel(model);
        
    }

    public void llenar_datos(){
        if (comprobanteId != 0) {
//            btnAddItem.setEnabled(true);
            ComprobanteDTO comprobanteDTO = (ComprobanteDTO) gestorComprobante.getById(comprobanteId);

            txtNumeroFactura.setText(String.valueOf(comprobanteDTO.getNumero()));
            txtDireccion.setText(comprobanteDTO.getDireccionCliente());
            txtRUC.setText(String.valueOf(comprobanteDTO.getRUCCliente()));
            txtNombre.setText(comprobanteDTO.getNombreCliente());
            dtcFecha.setDate(comprobanteDTO.getFingreso());
//            totalComprobante = Float.parseFloat(String.valueOf(comprobanteDTO.getTotal()));
            lblTotal.setText(String.valueOf(comprobanteDTO.getTotal()));

            
        }
    }

    public void guardar(){
        //Guardando el comprobante

//        ComprobanteDTO comprobanteDTO = new ComprobanteDTO();
//        comprobanteDTO.setId(comprobanteId);
//        comprobanteDTO.setClienteId(gestorCliente.getIdAtIndex(cmbCliente.getSelectedIndex()));
//        comprobanteDTO.setFingreso(dtcFecha.getDate());
//
//        comprobanteDTO.setNumero(Integer.parseInt(txtNumeroFactura.getText()));
//
//        comprobanteDTO.setTotal(Float.valueOf(lblTotal.getText()));
//
//        if (comprobanteId == 0) {
//            comprobanteId = gestorComprobante.getIdByNumero(comprobanteDTO.getNumero());
//        }

    }

    private boolean camposCompletos(){
        if(txtNumeroFactura.getText().length() != 0 &&
           txtDireccion.getText().length() != 0 &&
           txtNombre.getText().length() != 0 &&
           txtRUC.getText().length() != 0)
                return true;
        else  return false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNumeroFactura = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dtcFecha = new com.toedter.calendar.JDateChooser("yyyy/MM/dd", "####/##/##", '_');
        jLabel3 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtRUC = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        btnAddItem = new javax.swing.JButton();
        btnEliminarItem = new javax.swing.JButton();
        btnGuardarFactura = new javax.swing.JButton();
        btnGuias = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(FacturaDetalles.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtNumeroFactura.setText(resourceMap.getString("txtNumeroFactura.text")); // NOI18N
        txtNumeroFactura.setEnabled(false);
        txtNumeroFactura.setName("txtNumeroFactura"); // NOI18N
        txtNumeroFactura.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNumeroFacturaFocusLost(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        dtcFecha.setDate(new java.util.Date());
        dtcFecha.setEnabled(false);
        dtcFecha.setName("dtcFecha"); // NOI18N
        dtcFecha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                dtcFechaFocusLost(evt);
            }
        });

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtNombre.setEnabled(false);
        txtNombre.setName("txtNombre"); // NOI18N
        txtNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNombreFocusLost(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtDireccion.setEnabled(false);
        txtDireccion.setName("txtDireccion"); // NOI18N
        txtDireccion.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDireccionFocusLost(evt);
            }
        });

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtRUC.setEnabled(false);
        txtRUC.setName("txtRUC"); // NOI18N
        txtRUC.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtRUCFocusLost(evt);
            }
        });

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        lblTotal.setFont(resourceMap.getFont("lblTotal.font")); // NOI18N
        lblTotal.setText(resourceMap.getString("lblTotal.text")); // NOI18N
        lblTotal.setName("lblTotal"); // NOI18N

        btnAddItem.setText(resourceMap.getString("btnAddItem.text")); // NOI18N
        btnAddItem.setEnabled(false);
        btnAddItem.setName("btnAddItem"); // NOI18N
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        btnEliminarItem.setText(resourceMap.getString("btnEliminarItem.text")); // NOI18N
        btnEliminarItem.setEnabled(false);
        btnEliminarItem.setName("btnEliminarItem"); // NOI18N
        btnEliminarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarItemActionPerformed(evt);
            }
        });

        btnGuardarFactura.setText(resourceMap.getString("btnGuardarFactura.text")); // NOI18N
        btnGuardarFactura.setEnabled(false);
        btnGuardarFactura.setName("btnGuardarFactura"); // NOI18N
        btnGuardarFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarFacturaActionPerformed(evt);
            }
        });

        btnGuias.setText(resourceMap.getString("btnGuias.text")); // NOI18N
        btnGuias.setName("btnGuias"); // NOI18N
        btnGuias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuiasActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setEnabled(false);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(234, 234, 234)
                .addComponent(btnGuardarFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(328, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addComponent(btnAddItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEliminarItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGuias)
                .addGap(293, 293, 293))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTotal))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(39, 39, 39)
                                    .addComponent(jLabel2)
                                    .addGap(3, 3, 3)
                                    .addComponent(dtcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(txtDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                                .addComponent(txtNombre))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE))
                .addContainerGap(81, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(dtcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lblTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminarItem)
                    .addComponent(btnAddItem)
                    .addComponent(btnGuias))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardarFactura)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dtcFechaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dtcFechaFocusLost
//        this.cmbDescripcion.requestFocus();
}//GEN-LAST:event_dtcFechaFocusLost

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        // TODO add your handling code here:
        try {
                    if (camposCompletos()) {
            //Guardando los datos de la factura
            guardar();
            DialogFacturaDetalle dialogFacturaDetalle = new DialogFacturaDetalle(this, true, comprobanteId);
            dialogFacturaDetalle.setLocationRelativeTo(this);
            dialogFacturaDetalle.setVisible(true);
    //        System.out.println("Objeto: " + dialogFacturaDetalle.getObjeto());
//            totalComprobante += dialogFacturaDetalle.getComprobanteDetalleDTO().getTotal();
//            lista_comprobantes.add(dialogFacturaDetalle.getComprobanteDetalleDTO());

            Float nuevototal = Float.parseFloat(lblTotal.getText()) + dialogFacturaDetalle.getComprobanteDetalleDTO().getTotal();
            System.out.println("nuevo total: " + nuevototal);
            this.lblTotal.setText(String.valueOf(nuevototal));
            guardar();
            refresh();
            llenar_datos();
            manFactura.refresh();
//            System.out.println("Eementos: " + lista_comprobantes.size());

        } else {
            JOptionPane.showMessageDialog(this, "Deben estar llenos todos los campos de la factura", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
        } catch (DataIntegrityViolationException e) {
            JOptionPane.showMessageDialog(this, "Ya existe una factura con ese numero", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }





    }//GEN-LAST:event_btnAddItemActionPerformed

    private void btnGuardarFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarFacturaActionPerformed
        // TODO add your handling code here:
        guardar();
        llenar_datos();
        refresh();
        manFactura.refresh();
    }//GEN-LAST:event_btnGuardarFacturaActionPerformed

    private void btnEliminarItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarItemActionPerformed
        // TODO add your handling code here:
        try {
//            int i = this.jTable1.getSelectedRow();
//            System.out.println("Item: " + i);
//            totalComprobante -= lista_comprobantes.get(i).getTotal();
//            lista_comprobantes_borrados.add(lista_comprobantes.get(i));
//            gestorComprobanteDetalle.deleteById(lista_comprobantes.get(i).getId());
//            lista_comprobantes.remove(i);
            int i = Integer.parseInt(this.jTable1.getValueAt(this.jTable1.getSelectedRow(),0).toString());
            Float precio_eliminado = Float.parseFloat(jTable1.getValueAt(this.jTable1.getSelectedRow(), 4).toString());
            Float nuevototal = Float.parseFloat(lblTotal.getText()) - precio_eliminado;
            this.lblTotal.setText(String.valueOf(nuevototal));
//            ComprobanteDTO comprobanteDTO = (ComprobanteDTO) gestorComprobante.getById(comprobanteId);
//            comprobanteDTO.setTotal(comprobanteDTO.getTotal() - precio_eliminado);
//            gestorComprobante.saveOrUpdate(comprobanteDTO, viajeId);
            gestorComprobanteDetalle.deleteById(i);
            guardar();
            refresh();
            llenar_datos();
            manFactura.refresh();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un Item existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnEliminarItemActionPerformed

    private void btnGuiasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuiasActionPerformed
        // TODO add your handling code here:

//        guardar();
//        llenar_datos();
        ManGuia manGuia = new ManGuia(this, true, comprobanteId, lista_comprobantes, gestorComprobanteDetalle);
        manGuia.setLocationRelativeTo(this);
        manGuia.setVisible(true);
        refresh();
    }//GEN-LAST:event_btnGuiasActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        System.out.println("Comprobantes detalles: ");
        for (int i = 0; i < lista_comprobantes.size(); i++) {
            ComprobanteDetalleDTO comprobanteDetalleDTO = lista_comprobantes.get(i);
            System.out.println(comprobanteDetalleDTO.getProducto() + ", id " + comprobanteDetalleDTO.getGuiaId());
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtNumeroFacturaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNumeroFacturaFocusLost
        // TODO add your handling code here:

    }//GEN-LAST:event_txtNumeroFacturaFocusLost

    private void txtNombreFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNombreFocusLost
        // TODO add your handling code here:

    }//GEN-LAST:event_txtNombreFocusLost

    private void txtDireccionFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDireccionFocusLost
        // TODO add your handling code here:

    }//GEN-LAST:event_txtDireccionFocusLost

    private void txtRUCFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtRUCFocusLost

    }//GEN-LAST:event_txtRUCFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FacturaDetalles().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnEliminarItem;
    private javax.swing.JButton btnGuardarFactura;
    private javax.swing.JButton btnGuias;
    private com.toedter.calendar.JDateChooser dtcFecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumeroFactura;
    private javax.swing.JTextField txtRUC;
    // End of variables declaration//GEN-END:variables

    private DefaultTableModel model;
    private List<ComprobanteDetalleDTO> lista_comprobantes;
    private ApplicationContext ctx;
    private GestorComprobanteDetalle gestorComprobanteDetalle;
    private GestorComprobante gestorComprobante;
    private GestorCliente gestorCliente;
    private int comprobanteId;
    private int viajeId;
    ManFactura manFactura;
}
